package suzf.top.kotlinjava.service.controller

import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.util.Base64Utils
import org.springframework.web.client.RestTemplate



/**
 *
 *========================
 * Created with IntelliJ IDEA
 * Author:Mr.Su
 * Date:2019/9/28
 * Time:15:22
 * ========================
 */
fun main(args: Array<String>) {
    //  获取token
    val access_token_uri = "http://192.168.0.11:8889/oauth/token?grant_type=password&username=123&password=123"
    val clientId = "suzf"
    val clientSecret = "suzf"
    val basicAuth = "Basic %s";
    // 可以使用注入RestTemplate方式获取对象实例
    val restTemplate = RestTemplate()
    // 请求头
    val headers = HttpHeaders()
    // 设置客户端的basic认证信息
    headers["Authorization"] = String.format(basicAuth, Base64Utils.encodeToString("$clientId:$clientSecret".toByteArray()))
    // 请求主体
    val httpEntity = HttpEntity<String>(headers)
    // 发送请求，获取access_token
    val access_token = restTemplate.postForObject(access_token_uri, httpEntity, String::class.java)
    println(access_token)
}
